package myLucene;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;

import matrics.MapTermsToInteger;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class Lucene {

	/**
	 *
	 */
	HashMap<String, Integer> termsToInt;
	IndexReader iReader;
	HashMap<String, Integer> cf, df;
	HashMap<String, HashMap<Integer, LinkedList<String>>> freq;;

	/**
	 * @param termsToInt
	 *            : represents a mapping of the terms (String) to Integer
	 * @throws Exception
	 */
	public Lucene() throws Exception {
		//	
		// if(!Props.doIndexing){
		// Directory iDirectory = FSDirectory.open(new
		// File(Props.readIndexPath));
		// IndexSearcher searcher = new IndexSearcher(iDirectory, true);
		// iReader = searcher.getIndexReader();
		// }
		// termsToInt = MapTermsToInteger.getTermsToIntger(iReader);
		// cf = new HashMap<String, Integer>();
		// df = new HashMap<String, Integer>();
		// freq=Frequencies.getFreq(iReader, cf,df);
		this.start();
	}

	public void start() throws Exception {

		String writeIndexPath = "/local/joofeit/Index_" + GetTime.getTime();

		/**
		 * Indexing
		 */
		if (Props.doIndexing) {
			System.out.println("start indexing..." + GetTime.getTime());

			boolean success = (new File(writeIndexPath)).mkdir();
			if (success)
				System.out.println("Create indexDir: " + writeIndexPath);
			Indexer index = new Indexer(writeIndexPath, Props.dataPath);
			System.out.println("open index... " + GetTime.getTime());
			Directory iDirectory = FSDirectory.open(new File(
					Props.readIndexPath));
			IndexSearcher searcher = new IndexSearcher(iDirectory, true);
			iReader = searcher.getIndexReader();
			System.out.println("NUM DOCS: " + iReader.numDocs());
			iReader.close();
		}

		/**
		 * SEARCHING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		 */
		if (Props.doSearching) {
			// Searcher.search(iReader);
			Directory iDirectory = FSDirectory.open(new File(
					Props.readIndexPath));
			IndexSearcher searcher = new IndexSearcher(iDirectory, true);
			iReader = searcher.getIndexReader();
			System.out.println("NUM DOCS: " + iReader.numDocs());
			//Searcher.searchVerbs("7370040", iReader);
			iReader.close();
			// ReadQueries.readIdioms();

		}

		/**
		 * Print Dictionary
		 */
		if (Props.doFrequencies) {
			cf = new HashMap<String, Integer>();
			df = new HashMap<String, Integer>();
			freq = Frequencies.getFreq(iReader, cf, df);

			Frequencies.freqToString(cf, df, Props.freqPath, iReader);
			// Frequencies.SortedFreqToString(freq,freqPath);
			System.out
					.println("Number of documents: " + (iReader.maxDoc() - 1));
		}

	}

	public static IndexReader openIndex(Indexer index, Directory iDirectory)
			throws CorruptIndexException, IOException {

		IndexReader iReader = IndexReader.open(iDirectory);
		System.out.println("Opened Index...");
		return iReader;
	}

	public static String getDocToString(Document doc, String field) {
		String sent = "";

		String[] docVal = doc.getValues(field);

		for (int x = 0; x < docVal.length; x++) {
			sent = sent + " " + docVal[x] + "[" + x + "]";
		}

		return sent.trim();
	}

	public IndexReader getIndexReader() {
		return this.iReader;
	}

	public static HashSet<Integer> createStopWords() throws IOException {
		BufferedReader stopBR = new BufferedReader(new FileReader(new File(
				"../data/german_stop_words_sorted")));
		HashSet<Integer> stopWords = new HashSet<Integer>();
		String sw = "";

		while ((sw = stopBR.readLine()) != null) {
			stopWords.add(Integer.parseInt((sw.trim())));
		}
		stopBR.close();
		return stopWords;
	}

}
